package daoImpl;

import java.io.Serializable;

/**
 * ���߲�ѯ
 */

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pojo.Appuser;
import pojo.Watchuser;
import dao.UserDao;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	/*@Override
	public boolean login(Appuser appuser) {
		
		DetachedCriteria dc = DetachedCriteria.forClass(Appuser.class);
		if(appuser!=null){
			if(appuser.getUsername()!=null && appuser.getUsername().length()>0){
				dc.add(Restrictions.eq("username", appuser.getUsername()));
			}
			if(appuser.getPwd()!=null && appuser.getPwd().length()>0){		
				dc.add(Restrictions.eq("pwd", appuser.getPwd()));
			}
	
		}	
		List list = this.getHibernateTemplate().findByCriteria(dc);
		if(list!=null && list.size()>0){
			return true;
		}else{
			return false;
		}
		String username = appuser.getUsername();
		if(username==null || "".equals(username)){
			return false;
		}
		List list = this.getHibernateTemplate().find("from Appuser where username='"+username+"'");
		if(list.size()==0){
			return false;
		}else{
			//this.getHibernateTemplate().update("UPDATE appuser SET binder =(SELECT username FROM watchuser)");
			return true;
		}

		
	}*/
	@Override
	public Appuser login(Appuser appuser) {

		String username = appuser.getUsername();
		String pwd = appuser.getPwd();
		if(username==null || "".equals(username)){
			return null;
		}
		if(pwd==null || "".equals(pwd)){
			return null;
		}
		List list = this.getHibernateTemplate().find("from Appuser where username='"+username+"'");
		if(list.size()==0){
			return null;
		}else{
			//this.getHibernateTemplate().update("UPDATE appuser SET binder =(SELECT username FROM watchuser)");
			return (Appuser) list.get(0);
		}

		
	}

	@Override
	public void save(Appuser appuser) {
		this.getHibernateTemplate().save(appuser);
	}

	@Override
	public Appuser queryByUsername(String username) {
		if(username ==null || "".equals(username.trim())){
			 return null;
		}
		List<Appuser> list = this.getHibernateTemplate().find("from Appuser where username=?",username);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
		
	}

	@Override
	public Watchuser bind(Watchuser watchuser) {
				Appuser appuser=new Appuser();
			    this.getHibernateTemplate().save(watchuser);
			    //String username = appuser.getUsername();
				String hql="UPDATE Appuser SET binder = (SELECT username from Watchuser)";
				
				SessionFactory factory = this.getHibernateTemplate().getSessionFactory();
				Session session = factory.getCurrentSession();
				Query query = session.createQuery(hql);
				int i = query.executeUpdate();
				if(i<=0){
					return null;
				}
				return watchuser;

	}

	//根据手表用户的手机号码到数据库中查询是否存在
	@Override
	public Watchuser query(String binder) {
		if(binder == null || "".equals( binder.trim())){
			 return null;
		}
		List<Watchuser> list = this.getHibernateTemplate().find("from Watchuser where binder=?",binder);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
		
	}
	@Override
	public Watchuser queryUsername(String username) {
		if(username == null || "".equals( username.trim())){
			 return null;
		}
		List<Watchuser> list = this.getHibernateTemplate().find("from Watchuser where username=?",username);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
		
	}


	/* 
	 * @see dao.UserDao#delete(java.lang.String)
	 */
	@Override
	public void dele(String username) {
		// TODO Auto-generated method stub
		if(!"".equals(username.trim()) && username!=null){
		Watchuser watchuser = this.getHibernateTemplate().load(Watchuser.class, username);
			this.getHibernateTemplate().delete(watchuser);
		}
		String hql="UPDATE Appuser SET binder = (SELECT username from Watchuser)";
		SessionFactory factory = this.getHibernateTemplate().getSessionFactory();
		Session session = factory.getCurrentSession();
		Query query = session.createQuery(hql);
		 query.executeUpdate();
		
	}


	/* 查询绑定历史记录
	 * @see dao.UserDao#queryList(java.lang.String)
	 */
	@Override
	public List<Watchuser> queryList(String binder) {
		if(binder==null || "".equals(binder.trim())){//监护人为空
		return null;
		}
		List<Watchuser> list = this.getHibernateTemplate().find("from Watchuser where binder='"+binder+"'");
		if(list.size()==0){
			return null;
		}else{
			return list;
		}
		
	}

	/* (non-Javadoc)
	 * @see dao.UserDao#update(pojo.Appuser)
	 */
	@Override
	public Appuser update(Appuser appuser) {
		String username = appuser.getUsername();
		if(username==null){
			return null;
		}
		String hql="UPDATE Appuser SET binder = (SELECT username from Watchuser) WHERE username = '+username+'";
		
		SessionFactory factory = this.getHibernateTemplate().getSessionFactory();
		Session session = factory.getCurrentSession();
		Query query = session.createQuery(hql);
		int i = query.executeUpdate();
		if(i>0){
			return appuser;
		}else{
			return null;
		}

	}



}
